'use strict'
const {Sequelize,Model,Op,DataTypes} =require('sequelize')
const {db_database,db_dialect,db_host,db_password,db_username} = require('../config/db')
const fs = require('fs')

let sequelize = new Sequelize(db_database,db_username,db_password,{
    host:db_host,
    dialect:db_dialect
})
let res = {}
res.Op=Op
let files = fs.readdirSync(__dirname)
let readfile = files.filter(item=>{
    return item.endsWith('.js') && item !== 'index.js'
})
readfile.forEach(item=>{
    let modelname = item.replace('.js','')
    let tmpobj = require(__dirname+'/'+item)
    let tablename = modelname.toLowerCase()
    res[modelname]=sequelize.define(tablename,tmpobj)
})
res.sync=async()=>{
    sequelize.sync({force:true}).then(()=>{
        res.Product.bulkCreate([
            {
                name:'张三',
                price:200
            },
            {
                name:'李四',
                price:200
            },
            {
                name:'王五',
                price:1
            },
            
        ])
    })
}

module.exports=res